// pages/mine/mine.js
import { UserInfo } from "../../models/userinfo";
import { Factory } from "../../models/factory";
import { config } from "../../config/config";
Page({
  data: {
    show: false,
    Status: 0,
    registType: "",
    userInfo: null,
    img: "",
    bg: config.imgUrl + "me/bg.png?t=" + new Date(),
    config
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // wx.setNavigationBarTitle({
    //   title: '我的'
    // })
    let data = new UserInfo();
    this.data.userInfo = data.getUserInfo();
    console.log("this.data.userInfo");
    console.log(this.data.userInfo);
    this.setData({
      userInfo: data.getUserInfo()
    });
    if (this.data.userInfo && this.data.userInfo.RegistType == 2) {
      this.init();
    }
    // console.log(this.data.userInfo);
  },

  async init() {
    let postData = {
      Search: { all: true, userId: this.data.userInfo.Id, keyword: null },
      PageIndex: 1,
      PageRows: 10000,
      SortField: "id",
      SortType: "asc"
    };

    const data = await Factory.GetDataList(postData);

    if (data.Data.length > 0) {
      this.setData({
        Status: data.Data[0].Status,
        img: data.Data[0].Imags.split(",")[0]
      });
    }
  },

  goLogin() {
    wx.navigateTo({
      url: `/pages/login/login`
    });
  },

  publishOrder() {
    wx.navigateTo({
      url: `/pages/publish-order/publish-order`
    });
  },
  logout() {
    let data = new UserInfo();
    data.removeUserInfo();
    wx.nextTick(() => {
      this.setData({
        userInfo: data.getUserInfo()
      });
    });
  }
});
